//
//  String+Ext.swift
//  TimeManager
//
//  Created by 清风徐来 on 2023/10/7.
//

import UIKit

// MARK: - String扩展
extension String {
    
    /// 是否是有效的字符串（当是nil、""、空格、\n、null、<null>等，返回false）
    var isValid: Bool {
        guard !self.isEmpty else { return false }
        guard self != "null" else { return false }
        guard self != "nil" else { return false }
        guard self != "<null>" else { return false }
        
        var t = self.trimmingCharacters(in: .whitespacesAndNewlines)
        t = t.replacingOccurrences(of: " ", with: "")
        t = t.replacingOccurrences(of: "\r", with: "")
        t = t.replacingOccurrences(of: "\n", with: "")
        guard !t.isEmpty else { return false }
        return true
    }

    /// 截取range字符(不包含range)之前的所有字符
    func prefixLower(range: String) -> String {
        if let _range = self.range(of: range) {
            let newString = self.prefix(upTo: _range.lowerBound)
            return String(newString)
        } else {
            return self
        }
    }
}
